<?php
/*  
本周生产计划
*/
declare(strict_types=1);

namespace Modules\Pmcsignage\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Base\Repository\MaterialRepository;
use Modules\Production\Models\ProductionOrder;
use Modules\Production\Models\ProductionOrderDetail;
use Modules\Production\Repository\ProductionOrderRepository;
use Modules\Production\Repository\ProductionOrderStockUserRepository;
use Modules\Production\Service\ProductionOrderService;
use Modules\Pmcsignage\Http\Repositories\TodayproductionReget;
use Modules\Production\Models\ProductionReportOrder;
use Modules\Pmcsignage\Models\ProductionTheweekplan;
use Modules\Pmcsignage\Http\Repositories\ProductionTheweekReget;
use extend\services\warehouse\{warehouseService};//引入自定义类

class TheweekplanController extends Controller
{
    public function __construct(
        protected readonly ProductionTheweekplan $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
      
      
       
        $data = ProductionTheweekReget::listtable($request->all());
        $Plantype = ProductionTheweekplan::$Plantype;
        foreach($Plantype as $k=>$v){
            $label[$v['value']] = $v['label'];
        }
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
       
        foreach($data as $k=>$v){
            
            $ProductionOrder = ProductionOrder::select(
                'fbillno',
                'fmaterialid',
                'fmaterialname',
                'fline',
                'fworkshopid_name',
            )->where('id',$v['production_order_id'])->first();
            $data[$k]['fbillno'] = $ProductionOrder['fbillno'];
            $data[$k]['fmaterialid'] = $ProductionOrder['fmaterialid'];
            $data[$k]['fmaterialname'] = $ProductionOrder['fmaterialname'];
            $data[$k]['fline'] = $ProductionOrder['fline'];
            $data[$k]['fworkshopid_name'] = $ProductionOrder['fworkshopid_name'];

            $data[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
            $data[$k]['plantype'] = isset($label[$v['plantype']])?$label[$v['plantype']]:"";
           

            // (已领数量- 固定损耗)/ (分子/分母 ) * 1.05/
            $produ = ProductionOrderDetail::select(
                DB::raw('SUM(fpickedqty) as fpickedqty'),
                DB::raw('SUM(ffixscrapqty) as ffixscrapqty'),
                DB::raw('SUM(fnumerator) as fnumerator'),
                DB::raw('SUM(fdenominator) as fdenominator')
                )->where('production_order_id',$v['production_order_id'])
                ->groupby(['production_order_id'])->first();
                $back_number = 0.0000;
                if(!empty($produ)){
                    if($produ['ffixscrapqty'] > 0){
                        $kai = sprintf("%.4f",$produ['fpickedqty']/$produ['ffixscrapqty']);
                        $end = sprintf("%.4f",$produ['fnumerator']/$produ['fdenominator']);
                        $back_number = sprintf("%.4f",($kai/$end)* 1.05);
                    }
                }
                $data[$k]['back_number'] = $back_number;

        }
        return $data;
        //return $this->model->getList();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        return ProductionTheweekReget::create($request->all());
        // return $this->model->storeBy($request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $data = $this->model->firstBy($id);
        $ProductionOrder = ProductionOrder::select(
        'fbillno',
        'fmaterialid',
        'fmaterialname',
        'fline',
        'fworkshopid_name',
        )->where('id',$data['production_order_id'])->first();
        
        $data['fbillno'] = $ProductionOrder['fbillno'];
        $data['fmaterialid'] = $ProductionOrder['fmaterialid'];
        $data['fmaterialname'] = $ProductionOrder['fmaterialname'];
        $data['fline'] = $ProductionOrder['fline'];
        $data['fworkshopid_name'] = $ProductionOrder['fworkshopid_name'];
        
        return $data;
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        return $this->model->updateBy($id, $request->all());
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function plantype()
    {
        return ProductionTheweekplan::$Plantype;
    }

}
